//
//  SEHospitalizeServiceCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/2.
//

import UIKit

protocol SEHospitalizeServiceCellDelegate: NSObjectProtocol {
    func hospitalizeServiceCell(_ cell: SEHospitalizeServiceCell, didSeleectedWith departmentId: String)
}

class SEHospitalizeServiceCell: UITableViewCell {
    
    weak var delegate: SEHospitalizeServiceCellDelegate?
    
    private var viewModel: SEHospitalizeViewModel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        contentView.backgroundColor = .white
    }

    func update(with viewModel: SEHospitalizeViewModel) {
        self.viewModel = viewModel
        collectionView.reloadData()
        
        let contentSize = collectionView.collectionViewLayout.collectionViewContentSize
        collectionViewHeightConstraint.constant = contentSize.height
    }
    
    @IBOutlet private var collectionViewHeightConstraint: NSLayoutConstraint!
    
    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .white
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(SEDepartmentListCell.nibForCell(),
                                    forCellWithReuseIdentifier: SEDepartmentListCell.reuseIdentifier())
        }
    }
}

// MARK: - UICollectionViewDataSource

extension SEHospitalizeServiceCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.numberOfServiceItems ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEDepartmentListCell.reuseIdentifier(),
                                                      for: indexPath) as! SEDepartmentListCell
        if let vm = viewModel?.serviceCellViewModel(at: indexPath) {
            cell.update(with: vm)
        }
        return cell
    }
}

// MARK: - UICollectionViewDelegate & UICollectionViewDelegateFlowLayout

extension SEHospitalizeServiceCell: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        guard let vm = viewModel?.serviceCellViewModel(at: indexPath) else { return .zero }
        return vm.itemSize
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let vm = viewModel?.serviceCellViewModel(at: indexPath) else { return }
        delegate?.hospitalizeServiceCell(self, didSeleectedWith: vm.model.departmentId ?? "")
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? SEDepartmentListCell else { return }
        cell.setSelectedHighlight(true)
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? SEDepartmentListCell else { return }
        cell.setSelectedHighlight(false)
    }
}
